package com.api.provider;

import com.api.apicommon.server.InnerInterfaceUserInfo;
import com.api.model.domain.InterfaceInfo;
import com.api.model.domain.UserInterfaceInfo;
import com.api.service.InterfaceInfoService;
import com.api.service.UserInterfaceInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class InnerInterfaceUserInfoImpl implements InnerInterfaceUserInfo {
    @Autowired
    private UserInterfaceInfoService userInterfaceInfoService;
    @Autowired
    private InterfaceInfoService interfaceInfoService;

    /**
     * 更新接口统计次数
     *
     * @param userId
     * @param count
     * @return
     */
    @Override
    public boolean interfaceUserCount(Long userId, Integer count, String methodName, String url) {
        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url", url)
                .eq("method", methodName);
        InterfaceInfo interfaceInfo = interfaceInfoService.getOne(queryWrapper);
        Long interfaceInfoId = interfaceInfo.getId();
        UpdateWrapper<UserInterfaceInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("leftNum = leftNum + " + count + ",totalNum = totalNum + " + Math.abs(count))
                .eq("userId", userId)
                .eq("interfaceInfoId", interfaceInfoId)
                .gt("leftNum",0);
        return userInterfaceInfoService.update(updateWrapper);
    }
}
